/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
#include "StdAfx.h"

// org.pruessmann.perforce.core.api.Client

ClientApi* getClientApi(JNIEnv* env, jobject javaPerforceClient)
{
	return (ClientApi*)getNativeHandle(env, javaPerforceClient);
}

JNIEXPORT jint JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_dispose(JNIEnv* env, jobject jClientApi, jobject jError)
{
	ClientApi* clientApi = (ClientApi*)getNativeHandle(env, jClientApi);
	Error* error = (Error*)getNativeHandle(env, jError);

	return clientApi->Final(error);
}

JNIEXPORT void JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_setProtocol(JNIEnv* env, jobject jClientApi, jstring jProtocol, jstring jValue)
{
	ClientApi* clientApi = (ClientApi*)getNativeHandle(env, jClientApi);

	const char* protocol = env->GetStringUTFChars(jProtocol, JNI_FALSE);
	const char* value = env->GetStringUTFChars(jValue, JNI_FALSE);

	clientApi->SetProtocol(protocol, value);

	env->ReleaseStringUTFChars(jValue, value);
	env->ReleaseStringUTFChars(jProtocol, protocol);

}

JNIEXPORT jstring JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_getProtocol(JNIEnv* env, jobject jClientApi, jstring jProtocol)
{
	ClientApi* clientApi = (ClientApi*)getNativeHandle(env, jClientApi);

	const char* protocol = env->GetStringUTFChars(jProtocol, JNI_FALSE);
	StrPtr* value = clientApi->GetProtocol(protocol);
	env->ReleaseStringUTFChars(jProtocol, protocol);

	return env->NewStringUTF(value->Text());
}

JNIEXPORT void JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_init(JNIEnv* env, jobject javaPerforceClient, jobject javaPerforceError) 
{
	ClientApi* clientApi = getClientApi(env, javaPerforceClient);
	
	Error* error = (Error*)getNativeHandle(env, javaPerforceError);
	clientApi->Init(error); 
}

JNIEXPORT void JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_setArguments(JNIEnv* env, jobject jClientApi, jobjectArray jArguments)
{
	int size = env->GetArrayLength(jArguments);
	
	jstring* jArray = new jstring[size];
	char** args = new char*[size];
	for (int i = 0; i < size; i++)
	{
		jArray[i] = (jstring)env->GetObjectArrayElement(jArguments, i);
		args[i] = (char*)env->GetStringUTFChars(jArray[i], JNI_FALSE);
	}

	ClientApi* clientApi = (ClientApi*)getNativeHandle(env, jClientApi);
	clientApi->SetArgv(size, args);

#if defined(WIN32)
	for (i = 0; i < size; i++)
		env->ReleaseStringUTFChars(jArray[i], args[i]);
#else
	for (int i = 0; i < size; i++)
		env->ReleaseStringUTFChars(jArray[i], args[i]);
#endif

	delete [] args;
	delete [] jArray;
}

JNIEXPORT void JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_run(JNIEnv* env, jobject jClientApi, jstring jFunction, jobject jClientUser)
{
	ClientApi* clientApi = (ClientApi*)getNativeHandle(env, jClientApi);
	ClientUser* clientUser = (ClientUser*)getNativeHandle(env, jClientUser);

	const char* function = env->GetStringUTFChars(jFunction, JNI_FALSE);
	clientApi->Run(function, clientUser);
	env->ReleaseStringUTFChars(jFunction, function);

	jthrowable e = env->ExceptionOccurred();
	if (e)
	{
		env->ExceptionClear();
		env->Throw(e);
	}
}

JNIEXPORT jboolean JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_dropped(JNIEnv* env, jobject javaPerforceClient)
{
	ClientApi* clientApi = getClientApi(env, javaPerforceClient);
	return clientApi->Dropped() ? JNI_TRUE : JNI_FALSE;
}

JNIEXPORT void JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_setCharset(JNIEnv* env, jobject jClientApi, jstring jCharset)
{
	ClientApi* clientApi = (ClientApi*)getNativeHandle(env, jClientApi);

	const char* charset = env->GetStringUTFChars(jCharset, JNI_FALSE);
	clientApi->SetCharset(charset);
	env->ReleaseStringUTFChars(jCharset, charset);
}

JNIEXPORT jstring JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_getCharset(JNIEnv* env, jobject jClientApi)
{
	ClientApi* clientApi = (ClientApi*)getNativeHandle(env, jClientApi);
	return env->NewStringUTF(clientApi->GetCharset().Text());
}


JNIEXPORT void JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_setClient(JNIEnv* env, jobject jClientApi, jstring jClient)
{
	ClientApi* clientApi = (ClientApi*)getNativeHandle(env, jClientApi);

	const char* client = env->GetStringUTFChars(jClient, JNI_FALSE);
	clientApi->SetClient(client);
	env->ReleaseStringUTFChars(jClient, client);
}

JNIEXPORT jstring JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_getClient(JNIEnv* env, jobject jClientApi)
{
	ClientApi* clientApi = (ClientApi*)getNativeHandle(env, jClientApi);
	return env->NewStringUTF(clientApi->GetClient().Text());
}

JNIEXPORT void JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_setHost(JNIEnv* env, jobject jClientApi, jstring jHost)
{
	ClientApi* clientApi = (ClientApi*)getNativeHandle(env, jClientApi);

	const char* host = env->GetStringUTFChars(jHost, JNI_FALSE);
	clientApi->SetHost(host);
	env->ReleaseStringUTFChars(jHost, host);
}

JNIEXPORT jstring JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_getHost(JNIEnv* env, jobject jClientApi)
{
	ClientApi* clientApi = (ClientApi*)getNativeHandle(env, jClientApi);
	return env->NewStringUTF(clientApi->GetHost().Text());
}

JNIEXPORT void JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_setLanguage(JNIEnv* env, jobject jClientApi, jstring jLanguage)
{
	ClientApi* clientApi = (ClientApi*)getNativeHandle(env, jClientApi);

	const char* language = env->GetStringUTFChars(jLanguage, JNI_FALSE);
	clientApi->SetLanguage(language);
	env->ReleaseStringUTFChars(jLanguage, language);
}

JNIEXPORT jstring JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_getLanguage(JNIEnv* env, jobject jClientApi)
{
	ClientApi* clientApi = (ClientApi*)getNativeHandle(env, jClientApi);
	return env->NewStringUTF(clientApi->GetLanguage().Text());
}

JNIEXPORT void JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_setPassword(JNIEnv* env, jobject jClientApi, jstring jPassword)
{
	ClientApi* clientApi = (ClientApi*)getNativeHandle(env, jClientApi);

	const char* password = env->GetStringUTFChars(jPassword, JNI_FALSE);
	clientApi->SetPassword(password);
	env->ReleaseStringUTFChars(jPassword, password);
}

JNIEXPORT jstring JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_getPassword(JNIEnv* env, jobject jClientApi)
{
	ClientApi* clientApi = (ClientApi*)getNativeHandle(env, jClientApi);
	return env->NewStringUTF(clientApi->GetPassword().Text());
}

JNIEXPORT void JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_setPort(JNIEnv* env, jobject jClientApi, jstring jPort)
{
	ClientApi* clientApi = (ClientApi*)getNativeHandle(env, jClientApi);

	const char* port = env->GetStringUTFChars(jPort, JNI_FALSE);
	clientApi->SetPort(port);
	env->ReleaseStringUTFChars(jPort, port);
}

JNIEXPORT jstring JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_getPort(JNIEnv* env, jobject jClientApi)
{
	ClientApi* clientApi = (ClientApi*)getNativeHandle(env, jClientApi);
	return env->NewStringUTF(clientApi->GetPort().Text());
}

JNIEXPORT void JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_setUser(JNIEnv* env, jobject jClientApi, jstring jUser)
{
	ClientApi* clientApi = (ClientApi*)getNativeHandle(env, jClientApi);

	const char* user = env->GetStringUTFChars(jUser, JNI_FALSE);
	clientApi->SetUser(user);
	env->ReleaseStringUTFChars(jUser, user);
}

JNIEXPORT jstring JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_getUser(JNIEnv* env, jobject jClientApi)
{
	ClientApi* clientApi = (ClientApi*)getNativeHandle(env, jClientApi);
	return env->NewStringUTF(clientApi->GetUser().Text());
}

JNIEXPORT void JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_construct(JNIEnv* env, jobject javaPerforceClient)
{
	setNativeHandle(env, javaPerforceClient, new ClientApi());
}

JNIEXPORT void JNICALL Java_net_sourceforge_perforce_api_internal_ClientApi_destruct(JNIEnv* env, jobject javaPerforceClient)
{
	ClientApi* clientApi = getClientApi(env, javaPerforceClient);
	setNativeHandle(env, javaPerforceClient, NULL);

	
	delete clientApi;
}











